package util.comboloader;

import util.enumeration.Language;
import util.enumhelper.SupportedLocale;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;
import util.enumhelper.LocalizedLanguage;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean(name="enumLanguage")
@RequestScoped
public class EnumLanguage implements Serializable {
    private WebLocalization localization;
    //  user preferred locale
    private String preferredLocale;
    //The list with the items
    public List<SelectItem> listItems = null;


    /** Creates a new instance of EnumLanguage */
    public EnumLanguage() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        // load combobox
        loadCombo();
    }
    
    //Loading the items
    private void loadCombo() {
        listItems = new ArrayList<SelectItem>();
//        System.out.println("EL: " + preferredLocale);
        System.out.println("preferredLocale: " + preferredLocale);
        listItems.add(new SelectItem(null, LocalizedSelect.getLocalizedSelectAsString(preferredLocale)));

        for (Language l : Language.values()) {
            System.out.println("preferredLocale: " + preferredLocale);
            String localizedLanguage = LocalizedLanguage.getLocalizedLanguageAsString(l, preferredLocale);
            listItems.add(new SelectItem(SupportedLocale.getSupportedLocaleStringByLanguage(l), localizedLanguage));
        }
    }

    public List<SelectItem> getListItems() {
        return listItems;
    }

    public void setListItems(List<SelectItem> listItems) {
        this.listItems = listItems;
    }
    
}
